package com.test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TicketSource {

	private static volatile int currentTickek=30;
	static Lock lock =new ReentrantLock();

	public static int saleTicket(){
		lock.lock();
		try {
			if (currentTickek >0) {
				currentTickek--;
				if(currentTickek ==0) {
					System.out.println(Thread.currentThread().getName()
							+  "窗口出票成功"  +" 当前余票还有 " + currentTickek + " 今日卖完"
				 );
				}else{
					System.out.println(Thread.currentThread().getName()
							+  "窗口出票成功"  +" 当前余票还有 " + currentTickek); 
				}
			}else {
				System.out.println(Thread.currentThread().getName()+"窗口出票暂无余票");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			lock.unlock();	
		}
		
		return currentTickek;
	}
}
